<?php
/**
 * Created by PhpStorm.
 * User: xiaofeng
 * motto: 现在的努力是为了小时候吹过的牛逼！
 * Date: 2020/9/13
 * Time: 19:18
 */
namespace app\common\business;
use app\common\model\mysql\Category as CategoryModel;
class Category{
    public $model = null;
    public function __construct()
    {
        $this->model = new CategoryModel();
    }

    public function add($data){
        $data['status'] = config("status.mysql.table_normal");
        //根据$name 去查询是否存在这条记录
        try{
           $result = $this->model->save($data);
       }catch (\Exception $e){
           throw new \think\Exception("服务内部 异常");
       }

        return $result;
    }

    public function getNormalCategorys(){
        $filed = "id, name, pid";
       $categorys = $this->model->getNormalCategorys($filed);
       if (!$categorys){
          return [];
       }
       $categorys = $categorys->toArray();
        return $categorys;
    }

    public function getNormalAllCategorys(){
        $filed = "id as category_id, name, pid";
        $categorys = $this->model->getNormalCategorys($filed);
        if (!$categorys){
            return [];
        }
        $categorys = $categorys->toArray();
        return $categorys;
    }

    public function getLists($data,$num){
        $list = $this->model->getLists($data,$num);
        if (!$list){
            return [];
        }
        $result = $list->toArray();
        $result['render'] = $list->render();
        // 第一步拿到列表中pid 第二步： in mysql求count 第三步： 把count  填充到列表页
       $pids = array_column($result['data'],"id");

       if ($pids){
           $idCountResult = $this->model->getChildCountInPids(['pid'=>$pids]);
           $idCountResult = $idCountResult->toArray();

       $idCounts = [];
       // 第一种方式
            foreach ($idCountResult as $countResult){
                $idCounts[$countResult['pid']] = $countResult['count'];
            }
            if ($result['data']){
                foreach ($result['data'] as $k=>$value){
                    $result['data'][$k]['childCount'] = $idCounts[$value['id']] ?? 0;
                }
            }
       }
        return $result;
    }

    public function getById($id){
        $result = $this->model->find($id);
        if (empty($result)){
            return [];
        }

        $result = $result->toArray();
        return $result;
    }
    /**
     * 排序
     * @param $id
     * @param $listorder
     * @return bool
     * @throws \think\Exception
     */
    public function listorder($id, $listorder){
        // 查询 id这条数据是否存在
        $res = $this->getById($id);
        if (!$res){
            throw new \think\Exception("不存在该条记录");
        }
        $data = [
            "listorder"=>$listorder
        ];
        try{
            $res = $this->model->updateById($id,$data);
        }catch (\Exception $e){
            return false;
        }
        return $res;
    }

    public function status($id,$status){
        // 查询 ID这条数据是否存在
        $res = $this->getById($id);
        if (!$res){
            throw new \think\Exception();
        }
        if ($res['status'] == $status){
            throw new \think\Exception("状态修改前和修改后一样");
        }
        $data = [
            "status"=>intval($status)
        ];
        try{
            $res = $this->model->updateById($id,$data);
        }catch (\Exception $exception){
            //记录日志
            return false;
        }
        return $res;
    }

    /**
     * 获取一级分类的内容
     * @param int $pid
     * @param string $field
     * @return array
     */
    public function getNormalByPid($pid=0, $field = "id,name,pid"){

        try{
            $res = $this->model->getNormalByPid($pid,$field);
        }catch (\Exception $e){
            // 注意要写日志
            return [];
        }
        $res = $res->toArray();
        return $res;

    }
}
